const debounce = {
    mounted(el: any, binding: any) {
        // 将元素的事件处理函数替换为防抖后的函数
        // binding.value 是传入的 debounceDirective 方法
        // console.log('el==', el)
        // console.log('binding==', binding)
        el.addEventListener(binding.arg || 'click', debounceFn(binding.value, 1000));

    },
    // 在指令从元素移除时调用
    unmounted(el: any, binding: any) {
        el.removeEventListener(binding.arg || 'click', binding.value);
    }
}

// 防抖函数
const debounceFn = (fn: Function, delay: number) => {
    let timer = null as any;
    return function (...args: any[]) {
        // 清除之前的定时器
        if (timer) {
            clearTimeout(timer);
        }
        // 设置新的定时器，延迟后执行函数
        timer = setTimeout(() => {
            fn(...args);
        }, delay);
    };
};

export default debounce
